package gov.mogaha.ntis.cmm.mob.view;

import gov.mogaha.ntis.cmm.mob.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TitleView extends LinearLayout{

	View view;
	private TextView titleTextView;
	private final Context mContext;
	
	private Button 	rBtn;
	private Button	lBtn;
	
	public TitleView(Context context) {
		super(context);	
		mContext = context;
		init();
	}

	public TitleView(Context context, AttributeSet attrs) {
		super(context, attrs);	
		mContext = context;
		init();
	}
	
	public void init() {
		LayoutInflater li = (LayoutInflater) getContext().getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);
		view = li.inflate(R.layout.title_view, this, false);
		addView(view);
		
		titleTextView = (TextView)findViewById(R.id.title_text);
		titleTextView.setTypeface(null, Typeface.BOLD);
		titleTextView.setTypeface(Typeface.SERIF);
		lBtn = (Button)findViewById(R.id.l_btn);
		rBtn = (Button)findViewById(R.id.r_btn);
		
		lBtn.setVisibility(View.GONE);
		rBtn.setVisibility(View.GONE);
		
		rBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
				alert.setMessage(getResources().getString(R.string.gomain));
				alert.setPositiveButton(R.string.messagebox_ok, new DialogInterface.OnClickListener() {
				    public void onClick( DialogInterface dialog, int which) {
				    	//TitleView.this.mContext.startActivity(intent)
				    	
				    	Intent intent = new Intent();
				    	
				    	Activity activity = (Activity)mContext;
				    	activity.setResult(activity.RESULT_FIRST_USER, intent);
				    	activity.finish();
//				    	activity.setResult(activity.RESULT_FIRST_USER, intent);
//				    	activity.finish();
				    	//mContext.startActivity(intent)
				    	//메인으로 이동 
				    	//메인 이전의 다른 것들은 전부 죽인다
				    	//통합앱과의 연결은 살린다(처음 앱에 들어가 메인이 뜬것처럼)
				    }
				});
				alert.setNegativeButton(getResources().getString(R.string.messagebox_cancel), null);
				alert.show();
			}
		});
		
		lBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
		    	Activity activity = (Activity)mContext;
		    	activity.finish();
			}
		});
	}
	
	public void setTitle(String title) {
		titleTextView.setText(title);
	}
	
	public void setTitle(String title, int size) {
		titleTextView.setText(title);
		titleTextView.setTextSize(size);
	}
	
	public Button openLeftBtn() {
		lBtn.setVisibility(View.VISIBLE);
		return lBtn;
	}
	
	public Button openRightBtn() {
		rBtn.setVisibility(View.VISIBLE);
		return rBtn;
	}
	
	public void setBackgroundImage(int resid){
		titleTextView.setBackgroundResource(resid);
	}
}